package leader.tool;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.print.PrinterException;
import java.util.Vector;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;


public class JTableDefine extends JFrame {
	private MyTable table;
	private MyTable getJTable(String[][] columnNames,int[] px) {
		setBounds(10,10,2500000,2500000);
		JScrollPane scrollPane = new JScrollPane();   //支持滚动
		getContentPane().add(scrollPane,BorderLayout.CENTER);
		Vector columnNameV = new Vector();    //获得表头
		for(int column = 0;column<columnNames[0].length;column++) {
			columnNameV.add(columnNames[0][column]);
		}
		Vector tableValueV = new Vector();
		tableValueV.add(columnNameV);
		for(int row = 1;row<columnNames.length;row++) {//获得数据
			Vector rowV = new Vector();
			for(int column = 0;column<columnNames[row].length;column++) {
				rowV.add(columnNames[row][column]);  //数据
			}
			tableValueV.add(rowV);
		}
		table = new MyTable(tableValueV,columnNameV);   //自定义的表格
		table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);  //关闭表格列的自动调整功能。
		table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);   //单选
		table.setSelectionBackground(Color.YELLOW);
		table.setSelectionForeground(Color.RED);
		table.setRowHeight(30);
		scrollPane.setViewportView(table);   //支持滚动
		for(int i=0;i<px.length;i++) {
			table.getColumnModel().getColumn(i).setPreferredWidth(px[i]);
		}
			return table;
		}
	public void printTable(String[][] columnNames,int[] px) {
		getJTable(columnNames, px);
		try {
			table.print();
		} catch (PrinterException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
